package com.park.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.park.model.Permissions;
import com.park.model.RouteList;
import com.park.model.SysMenu;
import com.park.service.PermissionsService;
import com.park.service.SysMenuService;
import com.park.service.Utility;

@Controller
@RequestMapping("/permissions")
public class PermissionsController {
	@Autowired
	private PermissionsService permissionsService;
	@Autowired
	private SysMenuService sysMenuService;
	private static Log logger = LogFactory.getLog(PermissionsController.class);
	@RequestMapping(value = "/insert",method=RequestMethod.POST, produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public String insert(@RequestBody Permissions permissions) {
		logger.info("[insert]参数:"+permissions);
		Map<String,Object> result = new HashMap<String, Object>();
//		try {
			int count = permissionsService.insert(permissions);
		if(count > 0) {
			result.put("status", 1001);
			result.put("message", "添加成功");
		}else {
			result.put("status", 1002);
			result.put("message", "添加失败");
		}
//		}catch (Exception e) {
//			result.put("status", 1002);
//			result.put("message", "添加失败");
//		}
		return Utility.gson.toJson(result);
	}
	@RequestMapping(value = "/update",method=RequestMethod.POST, produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public String update(@RequestBody Permissions permissions) {
		logger.info("[update]参数:"+permissions);
		Map<String,Object> result = new HashMap<String, Object>();
//		try {
			int count = permissionsService.update(permissions);
		if(count > 0) {
			result.put("status", 1001);
			result.put("message", "修改成功");
		}else {
			result.put("status", 1002);
			result.put("message", "修改失败");
		}
//		}catch (Exception e) {
//			result.put("status", 1002);
//			result.put("message", "修改失败");
//		}
		return Utility.gson.toJson(result);
	}
	@RequestMapping(value = "/delete",method=RequestMethod.POST, produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public String delete(@RequestBody Map<String,Object> args) {
		logger.info("[delete]参数:"+args);
		int ids = (int)args.get("id");
		long id = Long.parseLong(String.valueOf(ids));
		Map<String,Object> result = new HashMap<String, Object>();
		try {
			int count = permissionsService.delete(id);
		if(count > 0) {
			result.put("status", 1001);
			result.put("message", "删除成功");
		}else {
			result.put("status", 1002);
			result.put("message", "删除失败");
		}
		}catch (Exception e) {
			result.put("status", 1002);
			result.put("message", "删除失败"+e);
		}
		return Utility.gson.toJson(result);
	}
	@RequestMapping(value = "/selectList",method=RequestMethod.POST, produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public String selectList(@RequestBody Map<String,Object> args) {
		logger.info("[selectList]参数:"+args);
		Map<String,Object> result = new HashMap<String, Object>();
		try {
		int startPage = (int)args.get("startPage");
		int pageSize = (int)args.get("pageSize");
		List<Permissions> roles = permissionsService.selectList((startPage-1)*pageSize,pageSize);
		result.put("status", 1001);
		result.put("message", "查询成功");
		result.put("body", roles);
		}catch (Exception e) {
			result.put("status", 1002);
			result.put("message", "查无数据");
		}
		return Utility.gson.toJson(result);
	}
	
	@RequestMapping(value = "/getRole",method=RequestMethod.POST, produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public String getRole(@RequestBody Map<String,Object> args) {
		logger.info("[getRole]参数:"+args);
		Map<String,Object> result = new HashMap<String, Object>();
		List<RouteList> routeLists = new ArrayList<RouteList>();
		String userName = (String)args.get("userName");
		List<Permissions> role = permissionsService.getByRoleName(userName);
		if(CollectionUtils.isNotEmpty(role)) {
			if(role.get(0).getName().contains("-#")) {
				int begin=role.get(0).getName().indexOf("-#");
				int last=role.get(0).getName().length();
				String userNameStr = role.get(0).getName().substring(begin, last);
				List<Permissions> ids = permissionsService.getByName(userNameStr);
				for(Permissions id : ids){
					RouteList route = new RouteList();
					route.setRole(id);
					List<SysMenu> routes = sysMenuService.getByRoleId(id.getId());
					route.setRouteIds(routes);
					routeLists.add(route);
				}
				result.put("status", 1001);
				result.put("message", "查询成功");
				result.put("body", routeLists);
				return Utility.gson.toJson(result);
			}else {
				List<Permissions> ids = permissionsService.getByName(null);
				for(Permissions id : ids){
					RouteList route = new RouteList();
					route.setRole(id);
					List<SysMenu> routes = sysMenuService.getByRoleId(id.getId());
					route.setRouteIds(routes);
					routeLists.add(route);
				}
				result.put("status", 1001);
				result.put("message", "查询成功");
				result.put("body", routeLists);
				return Utility.gson.toJson(result);
			}
		}else {
			result.put("status", 1001);
			result.put("message", "用户没绑定角色");
			result.put("body", routeLists);
			return Utility.gson.toJson(result);
		}
		
		
	}
}
